package rs.pnv.model.insurance;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.validator.constraints.NotBlank;

import rs.pnv.model.AbstractEntity;

@Entity
@Table(name = "SEP_HOUSE")
@XmlAccessorType(XmlAccessType.FIELD)
public class Home extends AbstractEntity {

	@XmlElement(nillable = false)
	@NotBlank
	@OneToOne
	@JoinColumn(nullable = false)
	private Owner owner;

	@NotBlank
	@Column(nullable = false)
	@XmlElement(nillable = false)
	private String address;

	@OneToOne(optional = true)
	private HomeInsurance homeInsurance;

	public Home() {

	}

	public Owner getOwner() {
		return owner;
	}

	public void setOwner(Owner owner) {
		this.owner = owner;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public HomeInsurance getHomeInsurance() {
		return homeInsurance;
	}

	public void setHomeInsurance(HomeInsurance homeInsurance) {
		this.homeInsurance = homeInsurance;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder()
				.append(owner)
				.append(address)
				.appendSuper(super.hashCode())
				.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}

		final Home otherHouse = (Home) obj;

		return new EqualsBuilder()
				.append(owner, otherHouse.getOwner())
				.append(address, otherHouse.getAddress())
				.appendSuper(super.equals(obj))
				.isEquals();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
				.append("owner", owner)
				.append("address", address)
				.append(super.toString())
				.toString();
	}

}
